<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>5.前端路由 | 星月前端博客</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="The top of the mountain is not the end, nor is the other mountain.">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.f057962c.css" as="style"><link rel="preload" href="/assets/js/app.b0d6e6c9.js" as="script"><link rel="preload" href="/assets/js/3.3f705cf6.js" as="script"><link rel="preload" href="/assets/js/1.5432b32a.js" as="script"><link rel="preload" href="/assets/js/60.e6987c2a.js" as="script"><link rel="prefetch" href="/assets/js/10.a8f43ca8.js"><link rel="prefetch" href="/assets/js/11.f3a618ea.js"><link rel="prefetch" href="/assets/js/12.b0d83b4c.js"><link rel="prefetch" href="/assets/js/13.f1fb4696.js"><link rel="prefetch" href="/assets/js/14.e5bc98a2.js"><link rel="prefetch" href="/assets/js/15.50519e37.js"><link rel="prefetch" href="/assets/js/16.3c6a720c.js"><link rel="prefetch" href="/assets/js/17.39fb3a22.js"><link rel="prefetch" href="/assets/js/18.76eb3752.js"><link rel="prefetch" href="/assets/js/19.7e9cf824.js"><link rel="prefetch" href="/assets/js/20.91bdeca4.js"><link rel="prefetch" href="/assets/js/21.136cc362.js"><link rel="prefetch" href="/assets/js/22.ec4f40f8.js"><link rel="prefetch" href="/assets/js/23.50127d3e.js"><link rel="prefetch" href="/assets/js/24.793b4c66.js"><link rel="prefetch" href="/assets/js/25.e2402b1b.js"><link rel="prefetch" href="/assets/js/26.cda4008e.js"><link rel="prefetch" href="/assets/js/27.8f1f302e.js"><link rel="prefetch" href="/assets/js/28.15cd65f2.js"><link rel="prefetch" href="/assets/js/29.00adce9e.js"><link rel="prefetch" href="/assets/js/30.c29d2b5d.js"><link rel="prefetch" href="/assets/js/31.40db97f7.js"><link rel="prefetch" href="/assets/js/32.70ed39cf.js"><link rel="prefetch" href="/assets/js/33.b0f7cfc3.js"><link rel="prefetch" href="/assets/js/34.f0a63370.js"><link rel="prefetch" href="/assets/js/35.88e20dad.js"><link rel="prefetch" href="/assets/js/36.13f74559.js"><link rel="prefetch" href="/assets/js/37.1d442fc4.js"><link rel="prefetch" href="/assets/js/38.84077197.js"><link rel="prefetch" href="/assets/js/39.51512938.js"><link rel="prefetch" href="/assets/js/4.3ce47df9.js"><link rel="prefetch" href="/assets/js/40.1f3d0e52.js"><link rel="prefetch" href="/assets/js/41.8e4ee580.js"><link rel="prefetch" href="/assets/js/42.690cbafe.js"><link rel="prefetch" href="/assets/js/43.c4aa2221.js"><link rel="prefetch" href="/assets/js/44.fb378084.js"><link rel="prefetch" href="/assets/js/45.77700693.js"><link rel="prefetch" href="/assets/js/46.d507cb48.js"><link rel="prefetch" href="/assets/js/47.e3a1008a.js"><link rel="prefetch" href="/assets/js/48.d6c9c78e.js"><link rel="prefetch" href="/assets/js/49.4667e7c0.js"><link rel="prefetch" href="/assets/js/5.6d641d4f.js"><link rel="prefetch" href="/assets/js/50.8ec4f4f5.js"><link rel="prefetch" href="/assets/js/51.d9305c51.js"><link rel="prefetch" href="/assets/js/52.db11222b.js"><link rel="prefetch" href="/assets/js/53.61d62d9d.js"><link rel="prefetch" href="/assets/js/54.f20b6c65.js"><link rel="prefetch" href="/assets/js/55.0a4f147e.js"><link rel="prefetch" href="/assets/js/56.cd32d35a.js"><link rel="prefetch" href="/assets/js/57.a5fde1bb.js"><link rel="prefetch" href="/assets/js/58.a0a97490.js"><link rel="prefetch" href="/assets/js/59.1bb4fab1.js"><link rel="prefetch" href="/assets/js/6.5f336290.js"><link rel="prefetch" href="/assets/js/61.419fcdc8.js"><link rel="prefetch" href="/assets/js/62.5a9d4e12.js"><link rel="prefetch" href="/assets/js/63.be6ca5ce.js"><link rel="prefetch" href="/assets/js/64.8aef8f94.js"><link rel="prefetch" href="/assets/js/65.16450833.js"><link rel="prefetch" href="/assets/js/66.2969e029.js"><link rel="prefetch" href="/assets/js/67.e3a3c472.js"><link rel="prefetch" href="/assets/js/68.6ff65261.js"><link rel="prefetch" href="/assets/js/69.cf4da88b.js"><link rel="prefetch" href="/assets/js/7.6a1d4e29.js"><link rel="prefetch" href="/assets/js/70.1472620a.js"><link rel="prefetch" href="/assets/js/71.1e7cb0a1.js"><link rel="prefetch" href="/assets/js/72.2bf85313.js"><link rel="prefetch" href="/assets/js/73.d9cb4d96.js"><link rel="prefetch" href="/assets/js/74.0c536977.js"><link rel="prefetch" href="/assets/js/75.4f82c6a8.js"><link rel="prefetch" href="/assets/js/76.42f3f0b7.js"><link rel="prefetch" href="/assets/js/77.70961c41.js"><link rel="prefetch" href="/assets/js/78.2f45f2fc.js"><link rel="prefetch" href="/assets/js/79.a68a9b1c.js"><link rel="prefetch" href="/assets/js/8.378e22b1.js"><link rel="prefetch" href="/assets/js/80.6cb339b8.js"><link rel="prefetch" href="/assets/js/81.5849f124.js"><link rel="prefetch" href="/assets/js/82.f5531d2d.js"><link rel="prefetch" href="/assets/js/83.47badffa.js"><link rel="prefetch" href="/assets/js/84.dece7e94.js"><link rel="prefetch" href="/assets/js/85.d9e7f122.js"><link rel="prefetch" href="/assets/js/86.233d7088.js"><link rel="prefetch" href="/assets/js/87.ce5dff2e.js"><link rel="prefetch" href="/assets/js/88.f976bef7.js"><link rel="prefetch" href="/assets/js/89.8473e540.js"><link rel="prefetch" href="/assets/js/9.9eb4a88c.js"><link rel="prefetch" href="/assets/js/90.d6288b36.js"><link rel="prefetch" href="/assets/js/91.989f4dea.js"><link rel="prefetch" href="/assets/js/92.6ff507f9.js">
    <link rel="stylesheet" href="/assets/css/0.styles.f057962c.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-5bb33761><div data-v-5bb33761><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-5bb33761 data-v-5bb33761><h3 class="title" data-v-59e6cb88>星月前端博客</h3> <p class="description" data-v-59e6cb88>The top of the mountain is not the end, nor is the other mountain.</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>星月</span>
          
        <!---->
        2022
      </a></span></div></div> <div class="hide" data-v-5bb33761><header class="navbar" data-v-5bb33761><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="星月前端博客" class="logo"> <span class="site-name">星月前端博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/ES6模块化与异步编程/" class="nav-link"><i class="undefined"></i>
  ES6模块化与异步编程
</a></li><li class="dropdown-item"><!----> <a href="/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/categories/TypeScript/" class="nav-link"><i class="undefined"></i>
  TypeScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/html/" class="nav-link"><i class="undefined"></i>
  html
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue2/" class="nav-link"><i class="undefined"></i>
  Vue2
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue3/" class="nav-link"><i class="undefined"></i>
  Vue3
</a></li><li class="dropdown-item"><!----> <a href="/categories/webpack/" class="nav-link"><i class="undefined"></i>
  webpack
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="/docs/theme-reco/" class="nav-link"><i class="iconfont reco-message"></i>
  随记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/858399075" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xingyueqianduan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  QQ:76120220
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  WX:15237156071
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-5bb33761></div> <aside class="sidebar" data-v-5bb33761><div class="personal-info-wrapper" data-v-1fad0c41 data-v-5bb33761><img src="/avatar.png" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    星月
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>82</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>9</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/ES6模块化与异步编程/" class="nav-link"><i class="undefined"></i>
  ES6模块化与异步编程
</a></li><li class="dropdown-item"><!----> <a href="/categories/css/" class="nav-link"><i class="undefined"></i>
  css
</a></li><li class="dropdown-item"><!----> <a href="/categories/TypeScript/" class="nav-link"><i class="undefined"></i>
  TypeScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/html/" class="nav-link"><i class="undefined"></i>
  html
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue2/" class="nav-link"><i class="undefined"></i>
  Vue2
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue3/" class="nav-link"><i class="undefined"></i>
  Vue3
</a></li><li class="dropdown-item"><!----> <a href="/categories/webpack/" class="nav-link"><i class="undefined"></i>
  webpack
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="/docs/theme-reco/" class="nav-link"><i class="iconfont reco-message"></i>
  随记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/858399075" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xingyueqianduan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  QQ:76120220
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://blog.yhxweb.top/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i></i>
  WX:15237156071
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-5bb33761><h3 class="title" data-v-59e6cb88>5.前端路由</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>星月</span>
          
        <!---->
        2022
      </a></span></div></div> <div data-v-5bb33761><div data-v-5bb33761><main class="page"><section style="display:;"><div class="page-title"><h1 class="title">5.前端路由</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>星月</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>8/1/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>vue</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="_5-路由"><a href="#_5-路由" class="header-anchor">#</a> 5 路由</h1> <h2 id="_5-1-前端路由的概念与原理"><a href="#_5-1-前端路由的概念与原理" class="header-anchor">#</a> 5.1 前端路由的概念与原理</h2> <h5 id="_5-1-1-什么是路由"><a href="#_5-1-1-什么是路由" class="header-anchor">#</a> 5.1.1 什么是路由</h5> <p>路由（英文：router）就是对应关系。</p> <h5 id="_5-1-2-spa-与前端路由"><a href="#_5-1-2-spa-与前端路由" class="header-anchor">#</a> 5.1.2  SPA 与前端路由</h5> <p>SPA 指的是一个 web 网站只有唯一的一个 HTML 页面，所有组件的展示与切换都在这唯一的一个页面内完成。</p> <p>此时，不同组件之间的切换需要通过前端路由来实现。</p> <p><strong>结论：在 SPA 项目中，不同功能之间的切换，要依赖于前端路由来完成！</strong></p> <h5 id="_5-1-3-什么是前端路由"><a href="#_5-1-3-什么是前端路由" class="header-anchor">#</a> 5.1.3 什么是前端路由</h5> <p>通俗易懂的概念：Hash 地址与组件之间的对应关系。</p> <p>在这里需要回顾一下瞄点（Hash）。</p> <p>html代码如下：</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>X-UA-Compatible<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>IE=edge<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>viewport<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>width=device-width, initial-scale=1.0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>Document<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.asid</span><span class="token punctuation">{</span>
      <span class="token property">position</span><span class="token punctuation">:</span> fixed<span class="token punctuation">;</span>
      <span class="token property">top</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
      <span class="token property">left</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> bisque<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">.box</span><span class="token punctuation">{</span>
      <span class="token property">height</span><span class="token punctuation">:</span> 800px<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">#box1</span><span class="token punctuation">{</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> pink<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">#box2</span><span class="token punctuation">{</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> skyblue<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">#box3</span><span class="token punctuation">{</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> orange<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token selector">#box4</span><span class="token punctuation">{</span>
      <span class="token property">background-color</span><span class="token punctuation">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span>230<span class="token punctuation">,</span> 111<span class="token punctuation">,</span> 111<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>asid<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#box1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>box1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#box2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>box2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#box3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>box3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#box4<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>box4<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box<span class="token punctuation">&quot;</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box<span class="token punctuation">&quot;</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box<span class="token punctuation">&quot;</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box<span class="token punctuation">&quot;</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>box4<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br></div></div><blockquote><p>当我们点击对应的box时候，跳转到对应的box，页面不会刷新，但是游览器会有前进后退的记录。在URL中会有#box*，叫做瞄点，在这里叫做Hash。</p></blockquote> <p>运行结果：</p> <p><img src="https://img-blog.csdnimg.cn/20bd09f38cb742dc9204374531f0eb30.png" alt="image-20220728164104388"></p> <h5 id="_5-1-4-前端路由的工作方式"><a href="#_5-1-4-前端路由的工作方式" class="header-anchor">#</a> 5.1.4 前端路由的工作方式</h5> <blockquote><p>用户点击了页面上的路由链接</p> <p>导致了 URL 地址栏中的 Hash 值发生了变化</p> <p>前端路由监听了到 Hash 地址的变化</p> <p>前端路由把当前 Hash 地址对应的组件渲染都浏览器中</p></blockquote> <p><img src="https://img-blog.csdnimg.cn/1979d12ee2064a49b884e512477852fe.png" alt="image-20220728180047924"></p> <h5 id="_5-1-5-实现简易的前端路由-了解"><a href="#_5-1-5-实现简易的前端路由-了解" class="header-anchor">#</a> 5.1.5 实现简易的前端路由(了解)</h5> <blockquote><div class="language-js line-numbers-mode"><pre class="language-js"><code>步骤<span class="token number">1</span>：通过 <span class="token operator">&lt;</span>component<span class="token operator">&gt;</span> 标签，结合 comName 动态渲染组件。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例代码如下：</p></blockquote> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token comment">&lt;!-- 通过 is 属性，指定要展示的组件的名称 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>component</span> <span class="token attr-name">:is</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>comName<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>component</span><span class="token punctuation">&gt;</span></span>

export default {
	name:'App',
	data(){
		return {
			//要展示的组件的名称
			comName:'Home'
		}
	}
}


</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><blockquote><div class="language-js line-numbers-mode"><pre class="language-js"><code>步骤<span class="token number">2</span>：在 App<span class="token punctuation">.</span>vue 组件中，为 <span class="token operator">&lt;</span>a<span class="token operator">&gt;</span> 链接添加对应的 hash 值：
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></blockquote> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#/home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#/movie<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Movie<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>;
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#/about<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>About<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><blockquote><div class="language-js line-numbers-mode"><pre class="language-js"><code>步骤<span class="token number">3</span>：在 created 生命周期函数中，监听浏览器地址栏中 hash 地址的变化，动态切换要展示的组件的名称：
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">created</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	window<span class="token punctuation">.</span><span class="token function-variable function">onhashchange</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span><span class="token punctuation">{</span>
		<span class="token keyword">switch</span><span class="token punctuation">(</span>location<span class="token punctuation">.</span>hash<span class="token punctuation">)</span><span class="token punctuation">{</span>
			<span class="token keyword">case</span> <span class="token string">'#/home'</span><span class="token operator">:</span> <span class="token comment">//点击了&quot;首页&quot;链接</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span>comName <span class="token operator">=</span> <span class="token string">'Home'</span>
				<span class="token keyword">break</span>
			<span class="token keyword">case</span> <span class="token string">'#/movie'</span> <span class="token operator">:</span> <span class="token comment">//点击了&quot;电影&quot;链接</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span>comName <span class="token operator">=</span> 'movie
				<span class="token keyword">break</span>
			<span class="token keyword">case</span> <span class="token string">'#/about'</span><span class="token operator">:</span> <span class="token comment">//点击了&quot;关于&quot;链接</span>
				<span class="token keyword">this</span><span class="token punctuation">.</span>comName <span class="token operator">=</span> <span class="token string">'about'</span>
				<span class="token keyword">break</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><blockquote><p>以上是前端路由底层原理，在开发时候当然不用自己配置前端路由，不然就好比炒菜还得先去搭灶台一样！</p> <p>下面进入vue-router 的基本使用。</p></blockquote> <h2 id="_5-2-vue-router-的基本使用"><a href="#_5-2-vue-router-的基本使用" class="header-anchor">#</a> 5.2 vue-router 的基本使用</h2> <h5 id="_5-2-1-什么是-vue-router"><a href="#_5-2-1-什么是-vue-router" class="header-anchor">#</a> 5.2.1 什么是 vue-router</h5> <p>​		vue-router 是 vue.js 官方给出的路由解决方案。它只能结合 vue 项目进行使用，能够轻松的管理 SPA 项目</p> <p>中组件的切换。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>vue<span class="token operator">-</span>router 的官方文档地址：
<span class="token literal-property property">https</span><span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>router<span class="token punctuation">.</span>vuejs<span class="token punctuation">.</span>org<span class="token operator">/</span>zh<span class="token operator">/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h5 id="_5-2-2-vue-router-安装和配置的步骤"><a href="#_5-2-2-vue-router-安装和配置的步骤" class="header-anchor">#</a> 5.2.2 vue-router 安装和配置的步骤</h5> <blockquote><p>① 安装 vue-router 包</p> <p>② 创建路由模块</p> <p>③ 导入并挂载路由模块</p> <p>④ 声明路由链接和占位符</p></blockquote> <ul><li><p><strong>在项目中安装 vue-router</strong></p> <p>在 vue2 的项目中，安装 vue-router 的命令如下：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>npm i vue<span class="token operator">-</span>router@<span class="token number">3.5</span><span class="token number">.2</span> <span class="token operator">-</span><span class="token constant">S</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p><strong>创建路由模块</strong></p> <p>在 src 源代码目录下，新建 router/index.js 路由模块，并初始化如下的代码：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//1.导入vue和router包</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>

 <span class="token comment">//2.调用Vue.use()函数，把 VueRouter 安装为 Vue 的插件</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>VueRouter<span class="token punctuation">)</span>

<span class="token comment">//3.创建路由的示例对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">//4.向外共享路由示例对象</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> router
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li> <li><p><strong>导入并挂载路由模块</strong></p> <p>在 src/main.js 入口文件中，导入并挂载路由模块。示例代码如下：</p></li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> App <span class="token keyword">from</span> <span class="token string">'./App.vue'</span>
<span class="token comment">//导入路由模块</span>
<span class="token keyword">import</span> router <span class="token keyword">from</span> <span class="token string">'@/router/index.js'</span>

<span class="token comment">// 导入 bootstrap 样式</span>
<span class="token keyword">import</span> <span class="token string">'bootstrap/dist/css/bootstrap.min.css'</span>
<span class="token comment">// 全局样式</span>
<span class="token keyword">import</span> <span class="token string">'@/assets/global.css'</span>

Vue<span class="token punctuation">.</span>config<span class="token punctuation">.</span>productionTip <span class="token operator">=</span> <span class="token boolean">false</span>

<span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token function-variable function">render</span><span class="token operator">:</span> <span class="token parameter">h</span> <span class="token operator">=&gt;</span> <span class="token function">h</span><span class="token punctuation">(</span>App<span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token comment">// router:router,注册路由，</span>
  <span class="token comment">//下面直接一个router是简写(导入的router和注册的router名字一样，可以直接简写成一个router)</span>
  router
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">$mount</span><span class="token punctuation">(</span><span class="token string">'#app'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><ul><li><p><strong>声明路由链接和占位符</strong></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>在 src<span class="token operator">/</span>App<span class="token punctuation">.</span>vue 组件中，使用 vue<span class="token operator">-</span>router 提供的 <span class="token operator">&lt;</span>router<span class="token operator">-</span>link<span class="token operator">&gt;</span> 和 <span class="token operator">&lt;</span>router<span class="token operator">-</span>view<span class="token operator">&gt;</span> 声明路由链接和占位符：
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>app-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>App2 组件<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- 只要在项目中安装了 vue-router 就可以使用 router-link 代替a链接 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>#/Home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>首页<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/Home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>首页<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/Movie<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/About<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>关于<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span> <span class="token punctuation">/&gt;</span></span>
    <span class="token comment">&lt;!-- 只要在项目中安装了 vue-router 就可以使用 router-view 这个组件 --&gt;</span>
    <span class="token comment">&lt;!-- 是一个占位符 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">&gt;</span></span>

  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'App'</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>less<span class="token punctuation">&quot;</span></span> <span class="token attr-name">scoped</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">.app-container</span> <span class="token punctuation">{</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> #efefef<span class="token punctuation">;</span>
  <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span>
  <span class="token property">margin</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 15px<span class="token punctuation">;</span>
  <span class="token selector">&gt; a</span> <span class="token punctuation">{</span>
    <span class="token property">margin-right</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><ul><li><strong>声明路由的匹配规则</strong></li></ul> <p>在 src/router/index.js 路由模块中，通过 routes 数组声明路由的匹配规则。示例代码如下：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//1.导入vue和router包</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>

<span class="token comment">//导入需要的组件</span>
<span class="token keyword">import</span> Home <span class="token keyword">from</span> <span class="token string">'@/components/Home.vue'</span>
<span class="token keyword">import</span> Movie <span class="token keyword">from</span> <span class="token string">'@/components/Movie.vue'</span>
<span class="token keyword">import</span> About <span class="token keyword">from</span> <span class="token string">'@/components/About.vue'</span>

 <span class="token comment">//2.调用Vue.use()函数，把 VueRouter 安装为 Vue 的插件</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>VueRouter<span class="token punctuation">)</span>

<span class="token comment">//3.创建路由的示例对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">routes</span><span class="token operator">:</span><span class="token punctuation">[</span>
    <span class="token comment">//在 routes 数组中，申明路由的匹配规则</span>
    <span class="token comment">//path 表示要匹配的 hash 地址；component 表示要展示的路由组件</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/Home'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Home<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/Movie'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/About'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>About<span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//4.向外共享路由示例对象</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> router

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>点击可以展示对应的组件，如图：</p> <p><img src="https://img-blog.csdnimg.cn/4207afc0f20c4ce5b3f3d3f9aa9f5396.png" alt="image-20220804212924334"></p> <p><img src="https://img-blog.csdnimg.cn/5242e102e98a4b1896ef39b85ab04cec.png" alt="image-20220804212846745"></p> <h2 id="_5-3-vue-router-的常见用法"><a href="#_5-3-vue-router-的常见用法" class="header-anchor">#</a> 5.3 vue-router 的常见用法</h2> <h5 id="_5-3-1-路由重定向"><a href="#_5-3-1-路由重定向" class="header-anchor">#</a> 5.3.1 路由重定向</h5> <p>路由重定向指的是：用户在访问地址 A 的时候，强制用户跳转到地址 C ，从而展示特定的组件页面。</p> <p>通过路由规则的 redirect 属性，指定一个新的路由地址，可以很方便地设置路由的重定向：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//3.创建路由的示例对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">routes</span><span class="token operator">:</span><span class="token punctuation">[</span>
    <span class="token comment">//路由重定向</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/'</span><span class="token punctuation">,</span><span class="token literal-property property">redirect</span><span class="token operator">:</span><span class="token string">'/Home'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token comment">//在 routes 数组中，申明路由的匹配规则</span>
    <span class="token comment">//path 表示要匹配的 hash 地址；component 表示要展示的路由组件</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/Home'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Home<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/Movie'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/About'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>About<span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>这样我们访问到hash地址为#/的时候就直接跳转到/Home！好处就是用户刚进入页面就显示Home组件。</p> <h5 id="_5-3-2-嵌套路由"><a href="#_5-3-2-嵌套路由" class="header-anchor">#</a> 5.3.2 嵌套路由</h5> <p>通过路由实现组件的嵌套展示，叫做嵌套路由。</p> <p>点击父级路由链接显示模板内容，如图：</p> <p><img src="https://img-blog.csdnimg.cn/086fa89a5acf4393844381053c46bf28.png" alt="image-20220804214009666"></p> <p>模板内容中又有子级路由链接，点击子级路由链接显示子级模板内容，如图：</p> <p><img src="https://img-blog.csdnimg.cn/1329d9ec2a434984b03a8fc080f61226.png" alt="image-20220804214048639"></p> <h5 id="_5-3-3-子路由"><a href="#_5-3-3-子路由" class="header-anchor">#</a> 5.3.3 子路由</h5> <p><strong>声明子路由链接和子路由占位符</strong></p> <p>在 About.vue 组件中，声明 tab1 和 tab2 的子路由链接以及子路由占位符。示例代码如下：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>about-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h3</span><span class="token punctuation">&gt;</span></span>About 组件<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h3</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- 子级路由链接 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/about/tab1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Tab1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/about/tab2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Tab2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">&gt;</span></span>

  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'About'</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>less<span class="token punctuation">&quot;</span></span> <span class="token attr-name">scoped</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">.about-container</span> <span class="token punctuation">{</span>
  <span class="token property">min-height</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> skyblue<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 15px<span class="token punctuation">;</span>
  <span class="token selector">&gt; a</span> <span class="token punctuation">{</span>
    <span class="token property">margin-right</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p><strong>通过 children 属性声明子路由规则</strong></p> <p>在 src/router/index.js 路由模块中，导入需要的组件，并使用 children 属性声明子路由规则：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//1.导入vue和router包</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>

<span class="token comment">//导入需要的组件</span>
<span class="token keyword">import</span> Home <span class="token keyword">from</span> <span class="token string">'@/components/Home.vue'</span>
<span class="token keyword">import</span> Movie <span class="token keyword">from</span> <span class="token string">'@/components/Movie.vue'</span>
<span class="token keyword">import</span> About <span class="token keyword">from</span> <span class="token string">'@/components/About.vue'</span>
<span class="token comment">//导入子路需要的由组件</span>
<span class="token keyword">import</span> Tab1 <span class="token keyword">from</span> <span class="token string">'@/components/tabs/Tab1.vue'</span>
<span class="token keyword">import</span> Tab2 <span class="token keyword">from</span> <span class="token string">'@/components/tabs/Tab2.vue'</span>

 <span class="token comment">//2.调用Vue.use()函数，把 VueRouter 安装为 Vue 的插件</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>VueRouter<span class="token punctuation">)</span>

<span class="token comment">//3.创建路由的示例对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">routes</span><span class="token operator">:</span><span class="token punctuation">[</span>
    <span class="token comment">//路由重定向</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/'</span><span class="token punctuation">,</span><span class="token literal-property property">redirect</span><span class="token operator">:</span><span class="token string">'/home'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token comment">//在 routes 数组中，申明路由的匹配规则</span>
    <span class="token comment">//path 表示要匹配的 hash 地址；component 表示要展示的路由组件</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/home'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Home<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> 
      <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/about'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">component</span><span class="token operator">:</span>About<span class="token punctuation">,</span>
      <span class="token literal-property property">children</span> <span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token comment">//子路由重定向</span>
        <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/'</span><span class="token punctuation">,</span><span class="token literal-property property">redirect</span><span class="token operator">:</span><span class="token string">'tab1'</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token comment">// 定义子路由规则</span>
        <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'tab1'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Tab1 <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'tab2'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Tab2<span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//4.向外共享路由示例对象</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> router
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><p>示例如下：</p> <p><img src="https://img-blog.csdnimg.cn/9d5213c537ae45fc80eafa051cf0f294.png" alt="image-20220804220844015"></p> <h5 id="_5-3-4-动态路由匹配"><a href="#_5-3-4-动态路由匹配" class="header-anchor">#</a> 5.3.4 动态路由匹配</h5> <p>学了上面的路由，思考：有如下 3 个路由链接：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>定义如下 3 个路由规则，是否可行???</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/1'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/2'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/3'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie <span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>可以，但是有缺点：路由规则的复用性差。如果有1万个电影，那么要定义1万个路由...</p> <ul><li><strong>动态路由的概念</strong></li></ul> <p>​	动态路由指的是：把 Hash 地址中可变的部分定义为参数项，从而提高路由规则的复用性。</p> <p>在 vue-router 中使用英文的冒号（:）来定义路由的参数项。</p> <p>示例代码如下：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//路由中的动态参数， 用 ： 来申明，冒号后面是动态参数的名称。</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/:id'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie<span class="token punctuation">}</span><span class="token punctuation">,</span>
        
<span class="token comment">//将以下路由合并为一个路由，提高复用性</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/1'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/2'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/3'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie <span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><strong>Movie 中代码如下：</strong></p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>movie-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h3</span><span class="token punctuation">&gt;</span></span>Movie 组件<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h3</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>ShowThis<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>打印this<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>这个电影对应的id为：{{ this.$route.params.id  }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Movie'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">methods</span><span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token function">ShowThis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>less<span class="token punctuation">&quot;</span></span> <span class="token attr-name">scoped</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">.movie-container</span> <span class="token punctuation">{</span>
  <span class="token property">min-height</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> lightsalmon<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 15px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p><strong>vue.app中部分代码如下：</strong></p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>app-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>App2 组件<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- 只要在项目中安装了 vue-router 就可以使用 router-link 代替a链接 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>首页<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/2<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/about<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>关于<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span> <span class="token punctuation">/&gt;</span></span>
    <span class="token comment">&lt;!-- 只要在项目中安装了 vue-router 就可以使用 router-view 这个组件 --&gt;</span>
    <span class="token comment">&lt;!-- 是一个占位符 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>$route.params</strong> <strong>参数对象</strong></p> <blockquote><div class="language-js line-numbers-mode"><pre class="language-js"><code>在动态路由渲染出来的组件中，可以使用 <span class="token keyword">this</span><span class="token punctuation">.</span>$route<span class="token punctuation">.</span>params 对象访问到动态匹配的参数值。
我们打印出<span class="token keyword">this</span>可以找到，$route<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id 就是这个路由的视频对应的id。
可以通过 <span class="token keyword">this</span><span class="token punctuation">.</span>$route<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id 来拿到这个id。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></blockquote> <ul><li><strong>使用 props 接收路由参数</strong></li></ul> <p>​	上面的方式也可以，但是为了简化路由参数的获取形式，vue-router 允许在路由规则中开启 props 传参。</p> <p>示例代码如下：</p> <ol><li>Movie 中代码如下：</li></ol> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>movie-container<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h3</span><span class="token punctuation">&gt;</span></span>Movie 组件<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h3</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>ShowThis<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>打印this<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>hr</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>这个电影对应的id为：{{ this.$route.params.id  }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>
      
      <span class="token comment">&lt;!-- 这里直接根据props中的 id 就可以拿到 id 的值了 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">&gt;</span></span>这个电影对应的id为：{{ id  }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">&gt;</span></span>

  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'Movie'</span><span class="token punctuation">,</span>
  <span class="token literal-property property">methods</span><span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token function">ShowThis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
   <span class="token comment">// 接收props数据</span>
  <span class="token literal-property property">props</span><span class="token operator">:</span><span class="token punctuation">[</span> <span class="token string">'id'</span> <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>less<span class="token punctuation">&quot;</span></span> <span class="token attr-name">scoped</span><span class="token punctuation">&gt;</span></span><span class="token style"><span class="token language-css">
<span class="token selector">.movie-container</span> <span class="token punctuation">{</span>
  <span class="token property">min-height</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span>
  <span class="token property">background-color</span><span class="token punctuation">:</span> lightsalmon<span class="token punctuation">;</span>
  <span class="token property">padding</span><span class="token punctuation">:</span> 15px<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><ol start="2"><li><p>router/index.js中部分代码：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span><span class="token string">'/movie/:id'</span><span class="token punctuation">,</span><span class="token literal-property property">component</span><span class="token operator">:</span>Movie<span class="token punctuation">,</span><span class="token literal-property property">props</span><span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ol> <p>一定要开 props:true 来传参，不然拿不到数据。</p> <p><strong>注意：</strong></p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code>	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>首页<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- 注意1：在 hash 地址中， / 后面的参数项，叫做“路径参数” --&gt;</span>
    <span class="token comment">&lt;!-- 在路由“参数对象”中，需要使用 this.$route.params 来访问路径参数 --&gt;</span>

    <span class="token comment">&lt;!-- 注意2：在 hash 地址中，? 后面的参数项，叫做“查询参数” --&gt;</span>
    <span class="token comment">&lt;!-- 在路由“参数对象”中，需要使用 this.$route.query 来访问查询参数 --&gt;</span>

    <span class="token comment">&lt;!-- 注意3：在 this.$route 中，path 只是路径部分；fullPath 是完整的地址 --&gt;</span>
    <span class="token comment">&lt;!-- 例如： --&gt;</span>
    <span class="token comment">&lt;!-- /movie/2?name=xingyue&amp;age=22 是 fullPath 的值 --&gt;</span>
    <span class="token comment">&lt;!-- /movie/2 是 path 的值 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/2?name=xingyue&amp;age=22<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/movie/3<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>电影3<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/about<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>关于<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><ul><li><strong>声明式导航 &amp; 编程式导航</strong></li></ul> <p>在浏览器中，点击链接实现导航的方式，叫做声明式导航。例如：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>普通网页中点击 <span class="token operator">&lt;</span>a<span class="token operator">&gt;</span> 链接、vue 项目中点击 <span class="token operator">&lt;</span>router<span class="token operator">-</span>link<span class="token operator">&gt;</span> 都属于声明式导航。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>在浏览器中，调用 API 方法实现导航的方式，叫做编程式导航。例如：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>普通网页中调用 location<span class="token punctuation">.</span>href 跳转到新页面的方式，属于编程式导航。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>vue-router 中的编程式导航 API</strong></p> <p>vue-router 提供了许多编程式导航的 API，其中最常用的导航 API 分别是：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>① <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'hash 地址'</span><span class="token punctuation">)</span>

		跳转到指定 hash 地址，并增加一条历史记录。
调用 <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 方法，可以跳转到指定的 hash 地址，从而展示对应的组件页面。

② <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'hash 地址'</span><span class="token punctuation">)</span> 

		跳转到指定的 hash 地址，并替换掉当前的历史记录。
调用 <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 方法，可以跳转到指定的 hash 地址，从而展示对应的组件页面。

③ <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">go</span><span class="token punctuation">(</span>数值 n<span class="token punctuation">)</span>

		实现导航历史前进、后退。
调用 <span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">go</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 方法，可以在浏览历史中前进和后退。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>push 和 replace 的区别：</p> <p>push 会增加一条历史记录</p> <p>replace 不会增加历史记录，而是替换掉当前的历史记录</p> <p><strong>$router.go的简化用法</strong></p> <p>在实际开发中，一般只会前进和后退一层页面。因此 vue-router 提供了如下两个便捷方法：</p> <p>① $router.back()      在历史记录中，后退到上一个页面。</p> <p>② $router.forward()	在历史记录中，前进到下一个页面。</p> <ul><li><strong>导航守卫</strong></li></ul> <p>导航守卫可以控制路由的访问权限。示意图如下：</p> <p><img src="https://img-blog.csdnimg.cn/0c1474c1915b4c81b51a1add082a0050.png" alt="image-20220805135817682"></p> <p><strong>全局前置守卫</strong></p> <p>每次发生路由的导航跳转时，都会触发全局前置守卫。因此，在全局前置守卫中，程序员可以对每个路由进行</p> <p>访问权限的控制：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//3.创建路由的示例对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//调用beforeEach()方法即可声明‘全局前置守卫’</span>
<span class="token comment">//每次发生路由跳转的时候都会触发这个fn函数</span>
router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>守卫方法的 3 个形参</strong></p> <p>全局前置守卫的回调函数中接收 3 个形参，格式为：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//3.创建路由的示例对象</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//调用beforeEach()方法即可声明‘全局前置守卫’</span>
<span class="token comment">//每次发生路由跳转的时候都会触发这个fn函数</span>
router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span>from<span class="token punctuation">,</span>next</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
  <span class="token comment">//to是将要访问的路由信息对象</span>
  <span class="token comment">//from是将要离开的路由信息对象</span>
  <span class="token comment">//next是一个函数，调用next()函数表示放行，允许这次路由导航</span>
  <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><strong>next 函数的 3 种调用方式</strong></p> <p>参考示意图，分析 next 函数的 3 种调用方式最终导致的结果：</p> <p><img src="https://img-blog.csdnimg.cn/8ae2381d76da4b7b8b304de59914f805.png" alt="image-20220805144228986"></p> <p>当前用户拥有后台主页的访问权限，直接放行：next()</p> <p>当前用户没有后台主页的访问权限，强制其跳转到登录页面：next('/login')</p> <p>当前用户没有后台主页的访问权限，不允许跳转到后台主页：next(false)</p> <p><strong>控制后台主页的访问权限</strong></p> <p>路由模块中，实现访问后台页面，通过判断token，没有登录则强制跳转到login登录页面。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span>from<span class="token punctuation">,</span>next</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
  <span class="token comment">//to是将要访问的路由信息对象</span>
  <span class="token comment">//from是将要离开的路由信息对象</span>
  <span class="token comment">//next是一个函数，调用next()函数表示放行，允许这次路由导航</span>
  <span class="token comment">// 分析：</span>
  <span class="token comment">// 1. 要拿到用户将要访问的 hash 地址</span>
  <span class="token comment">// 2. 判断 hash 地址是否等于 /main。</span>
  <span class="token comment">// 2.1 如果等于 /main，证明需要登录之后，才能访问成功</span>
  <span class="token comment">// 2.2 如果不等于 /main，则不需要登录，直接放行  next()</span>
  <span class="token comment">// 3. 如果访问的地址是 /main。则需要读取 localStorage 中的 token 值</span>
  <span class="token comment">// 3.1 如果有 token，则放行</span>
  <span class="token comment">// 3.2 如果没有 token，则强制跳转到 /login 登录页</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>path <span class="token operator">===</span> <span class="token string">'/main'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">//访问后台主页需要判断是否有token</span>
    <span class="token keyword">const</span> token <span class="token operator">=</span> localStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">'token'</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
      <span class="token function">next</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
    <span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">8/15/2022, 10:36:03 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-ac050c62><li class="level-2" data-v-ac050c62><a href="/blogs/vue2/2022/5.%E5%89%8D%E7%AB%AF%E8%B7%AF%E7%94%B1.html#_5-1-前端路由的概念与原理" class="sidebar-link reco-side-_5-1-前端路由的概念与原理" data-v-ac050c62>5.1 前端路由的概念与原理</a></li><li class="level-2" data-v-ac050c62><a href="/blogs/vue2/2022/5.%E5%89%8D%E7%AB%AF%E8%B7%AF%E7%94%B1.html#_5-2-vue-router-的基本使用" class="sidebar-link reco-side-_5-2-vue-router-的基本使用" data-v-ac050c62>5.2 vue-router 的基本使用</a></li><li class="level-2" data-v-ac050c62><a href="/blogs/vue2/2022/5.%E5%89%8D%E7%AB%AF%E8%B7%AF%E7%94%B1.html#_5-3-vue-router-的常见用法" class="sidebar-link reco-side-_5-3-vue-router-的常见用法" data-v-ac050c62>5.3 vue-router 的常见用法</a></li></ul></main></div> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      欢迎来到 星月前端博客
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:none;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="220" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.b0d6e6c9.js" defer></script><script src="/assets/js/3.3f705cf6.js" defer></script><script src="/assets/js/1.5432b32a.js" defer></script><script src="/assets/js/60.e6987c2a.js" defer></script>
  </body>
</html>
